package com.company.card_service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.card_service.mapper.IUserInfoMapper;
import com.company.card_service.model.User;
import com.company.card_service.util.AesUtil;
import com.company.card_service.vo.reqvo.user.UserSaveUpdateReqVo;
import com.company.card_service.vo.reqvo.user.modifyPasswordVo;
import org.springframework.stereotype.Repository;

/**
 * @program:
 * @description:
 * @author: lijunlin
 * @create: 2019/12/31 15:28
 **/
@Repository
public class UserRepository extends ServiceImpl<IUserInfoMapper, User> {

    public User getUserByAccountPhone(UserSaveUpdateReqVo reqVo){
        User user = this.getOne(Wrappers.<User>lambdaQuery()
                        .and(wrapper -> wrapper
                                .eq(User::getAccount, reqVo.getAccount())
                                .or()
                                .eq(User::getPhone, reqVo.getPhone()))
                .last("limit 1")
        );
        return user;
    }


    public Boolean modifyPass(modifyPasswordVo modifyPasswordVo) {

        boolean b = false;
        if(ObjectUtils.isNotEmpty(modifyPasswordVo)){
            User byId = this.getById(modifyPasswordVo.getId());
            String oldDecrypt = AesUtil.encrypt(modifyPasswordVo.getOldPassword());
            String newDecrypt = AesUtil.encrypt(modifyPasswordVo.getNewPassword());
            if(byId.getPassword() != null && byId.getPassword().equals(oldDecrypt)){
                byId.setPassword(newDecrypt);
                b = this.updateById(byId);
            }
        }
        return b;
    }
}
